package management;

import com.toedter.calendar.JDateChooser;
import wty.Login;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Add extends JFrame {
    JPanel AddPanel;
    JLabel ProjectLabel;
    JLabel Timeabel;
    JLabel NumberLebel;
    JLabel Categoryabel;
    JTextField ProjectText;
    // 使用JDateChooser替换原来的时间文本框
    JDateChooser dateChooser;
    JTextField NumberText;
    JComboBox<String> CategoryBox;
    JButton AddButton;
    Connection connection;
    PreparedStatement preparedStatement;
    int f = 0;

    // 添加消费信息的窗口
    public void AddFace() {
        AddPanel = new JPanel();
        setContentPane(AddPanel);
        this.setTitle("添加消费信息");
        setLayout(null);
        AddPanel.setBackground(Color.lightGray);

        ProjectLabel = new JLabel("消费项目");
        ProjectLabel.setBounds(90, 40, 100, 100);
        AddPanel.add(ProjectLabel);

        Timeabel = new JLabel("消费时间");
        Timeabel.setBounds(90, 90, 80, 80);
        AddPanel.add(Timeabel);

        // 添加日期选择组件
        dateChooser = new JDateChooser();
        dateChooser.setBounds(150, 123, 180, 20);
        AddPanel.add(dateChooser);

        NumberLebel = new JLabel("消费金额");
        NumberLebel.setBounds(90, 140, 80, 80);
        AddPanel.add(NumberLebel);

        Categoryabel = new JLabel("消费类型");
        Categoryabel.setBounds(90, 190, 80, 80);
        AddPanel.add(Categoryabel);

        ProjectText = new JTextField();
        ProjectText.setBounds(150, 80, 180, 20);
        AddPanel.add(ProjectText);

        NumberText = new JTextField();
        NumberText.setBounds(150, 173, 180, 20);
        AddPanel.add(NumberText);

        CategoryBox = new JComboBox<>();
        Category category = new Category();
        List<String> list = new ArrayList<>();
        list = category.getCategories();
        for (int i = 0, listSize = list.size(); i < listSize; i++) {
            CategoryBox.addItem(list.get(i));
        }
        CategoryBox.setBounds(150, 223, 100, 20);
        AddPanel.add(CategoryBox);

        AddButton = new JButton("添加");
        AddButton.setBounds(250, 380, 100, 30);
        AddPanel.add(AddButton);

        csql();
        AddButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 校验各个输入框是否为空
                if (ProjectText.getText().trim().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "消费项目不能为空，请填写完整", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (dateChooser.getDate() == null) {
                    JOptionPane.showMessageDialog(null, "消费时间不能为空，请选择日期", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (NumberText.getText().trim().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "消费金额不能为空，请填写完整", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (CategoryBox.getSelectedItem() == null) {
                    JOptionPane.showMessageDialog(null, "请选择消费类型", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                addConsumption();
            }
        });

        setBounds(400, 300, 640, 456);
        setVisible(true);
    }

    public void csql() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.err.println("加载错误");
        }
        String username = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/project";
        try {
            connection = DriverManager.getConnection(
                    url,
                    username,
                    password
            );
        } catch (
                SQLException throwables) {
            throwables.printStackTrace();
            System.err.println("数据库在连接期间，出现错误！");
        }
    }

    public void addConsumption() {
        String sql = "INSERT INTO consumer (consumer_id,consumption_project, date_time, number, category) VALUES (?,?,?,?,?)";

        try {

            double num = Double.parseDouble(NumberText.getText());
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, Management.getID());
            preparedStatement.setString(2, ProjectText.getText());
            // 获取日期选择组件选择的日期，并格式化为合适的字符串格式
            java.util.Date selectedDate = dateChooser.getDate();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String formattedDate = sdf.format(selectedDate);
                preparedStatement.setString(3, formattedDate);
            preparedStatement.setDouble(4, num);
            preparedStatement.setString(5, (String) CategoryBox.getSelectedItem());
            preparedStatement.executeUpdate();
            JOptionPane.showMessageDialog(null, "添加消费信息成功！");
            ProjectText.setText("");
            dateChooser.setDate(null);
            NumberText.setText("");
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, "消费金额请输入有效的数字格式", "错误", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "添加消费信息到数据库时出现错误，请检查输入内容是否符合要求", "错误", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Add add = new Add();
        add.AddFace();
    }
}